﻿using System;
using System.Windows;
using System.Windows.Input;
using System.ComponentModel;
using System.Windows.Controls;
using System.Collections.Generic;

using iTunesPwned.Core;
using iTunesPwned.Core.Plugin;

namespace iTunesPwned.UI
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Nightingale : Window
    {
        private iEngine m_engine;
        private System.Windows.Forms.NotifyIcon m_notifyIcon;

        public Nightingale()
        {
            InitializeComponent();
        }

        private void Window_Initialized(object sender, EventArgs e)
        {
            this.m_notifyIcon = new System.Windows.Forms.NotifyIcon();

            this.m_notifyIcon.Visible = true;
            this.m_notifyIcon.Text = "iTunesPwned";
            this.m_notifyIcon.Icon = new System.Drawing.Icon("appicon.ico");
            this.m_notifyIcon.DoubleClick += new EventHandler(m_notifyIcon_DoubleClick);

            this.m_engine = new iEngine();

            this.m_engine.RegisterTabItemHandler = new RegisterTabItemDelegate(this.RegisterTabItemHandler);

            this.m_engine.RegisterMenuItemHandler = new RegisterMenuItemDelegate(this.RegisterMenuItemHandler);

            this.m_engine.Init();
        }

        private void RegisterTabItemHandler(object sender, TabItem item)
        {
            if (!this.m_pluginTabControl.Items.Contains(item))
            {
                this.m_pluginTabControl.Items.Add(item);
            }

            this.m_pluginTabControl.SelectedItem = item;
        }

        private void RegisterMenuItemHandler(object sender, string name, RoutedEventHandler handler)
        {
            if (!this.menuItemPlugin.IsVisible)
                this.menuItemPlugin.Visibility = Visibility.Visible;

            IPlugin plugin = (IPlugin)sender;

            MenuItem parent = new MenuItem();
            parent.Header = plugin.Name;

            MenuItem item = new MenuItem();
            item.Header = name;
            item.Click += handler;

            parent.Items.Add(item);

            this.menuItemPlugin.Items.Add(parent);
        }

        private void m_notifyIcon_DoubleClick(object sender, EventArgs e)
        {
            this.Show();
        }

        private void menuItemExit_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void Window_Closing(object sender, CancelEventArgs e)
        {
            this.m_notifyIcon.Visible = false;

            this.m_engine.Dispose();
        }

        private void Window_StateChanged(object sender, EventArgs e)
        {
            switch (this.WindowState)
            {
                case WindowState.Minimized:
                    {
                        this.Hide();
                    }
                    break;
            }
        }

        private void closeMenuItem_Click(object sender, RoutedEventArgs e)
        {
            TabItem item = this.m_pluginTabControl.SelectedItem as TabItem;

            if ((bool)item.Tag)
                this.m_pluginTabControl.Items.RemoveAt(this.m_pluginTabControl.SelectedIndex);
        }
    }
}
